import time
import sys

from urlConfig.urlList import get_private_message_url, get_all_message_by_name_url
from utils.http import do_request


def get_unread_info():
    while True:
        time.sleep(1)
        from config import settings
        # print(12)
        if not settings.IsLogined:
            continue
        # print(15)
        if settings.IsClosed:
            sys.exit(0)
        # print(18)
        if len(settings.jwtToken) == 0:
            continue
        headers = {
            "Authorization": settings.jwtToken
        }
        response = do_request(method="get", url=get_private_message_url, headers=headers)
        if len(response.text) > 0:
            resp = response.json()
            sender = resp.get("result", {}).get("from")
            message = resp.get("result", {}).get("message")
            timestamp = resp.get("result", {}).get("time_stamp")
            print(f"{timestamp} - {sender} 给你发了一条消息: {message}")
        else:
            continue


def get_all_unread_info():
    while True:
        time.sleep(1)
        from config import settings
        if len(settings.jwtToken) == 0:
            continue
        headers = {
            "Authorization": settings.jwtToken
        }
        response = do_request(method="get", url=get_all_message_by_name_url, headers=headers)
        if len(response.text) > 0:
            # print(response.text)
            resp = response.json()
            sender = resp.get("result", {}).get("from")
            message = resp.get("result", {}).get("message")
            timestamp = resp.get("result", {}).get("time_stamp")
            print(f"{timestamp} - {sender} 给你发了一条消息: {message}")
        else:
            continue